package com.ldn.basic.utils;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.net.InetAddress;

/**
 * @author 李大牛家的牛 (scafel@163.com) 
 * @createDate 2024/7/12 - 9:32
 * @description 获取请求网络工具类
 **/
public class NetWorkUtil {

    /**
     * @author 李大牛家的牛(scafel@163.com)
     * @description 获取请求IP
     * @title getIpAddr
     * @date 2024/7/12 - 9:34
     * @param request:
     * @return java.lang.String
     **/
    public static String getIpAddr(javax.servlet.http.HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        // 如果获取到的IP为空或者为空字符串或者等于"unknown"，则获取"Proxy-Client-IP"头部信息
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        // 如果获取到的IP仍为空或者为空字符串或者等于"unknown"，则获取"WL-Proxy-Client-IP"头部信息
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        // 如果获取到的IP仍为空或者为空字符串或者等于"unknown"，则获取远程地址，同时判断是否为本地地址"127.0.0.1"
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            if (ip.equals("127.0.0.1")) {
                //根据网卡取本机配置的IP
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                    ip = inet.getHostAddress();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        // 多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        if (ip != null && ip.length() > 15) {
            if (ip.indexOf(",") > 0) {
                ip = ip.substring(0, ip.indexOf(","));
            }
        }
        return ip;
    }

    /**
     * @author 李大牛家的牛(scafel@163.com)
     * @description 获取请求IP
     * @title getIpAddr
     * @date 2024/7/12 - 9:47  
     * @return java.lang.String
     **/
    public static String getIpAddr() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            return null;
        }
        jakarta.servlet.http.HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        String ip = request.getHeader("x-forwarded-for");
        // 如果获取到的IP为空或者为空字符串或者等于"unknown"，则获取"Proxy-Client-IP"头部信息
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        // 如果获取到的IP仍为空或者为空字符串或者等于"unknown"，则获取"WL-Proxy-Client-IP"头部信息
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        // 如果获取到的IP仍为空或者为空字符串或者等于"unknown"，则获取远程地址，同时判断是否为本地地址"127.0.0.1"
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            if (ip.equals("127.0.0.1")) {
                //根据网卡取本机配置的IP
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                    ip = inet.getHostAddress();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        // 多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        if (ip != null && ip.length() > 15) {
            if (ip.indexOf(",") > 0) {
                ip = ip.substring(0, ip.indexOf(","));
            }
        }
        return ip;
    }


    public static String getOsName(javax.servlet.http.HttpServletRequest request) {
        String userAgentHeader = request.getHeader("user-agent");
        return getOsNameByHeader(userAgentHeader);
    }

    public static String getOsName() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            return null;
        }
        jakarta.servlet.http.HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        String userAgentHeader = request.getHeader("user-agent");
        return getOsNameByHeader(userAgentHeader);
    }

    private static String getOsNameByHeader(String userAgentHeader) {
        // 如果User-Agent字符串长度超过200个字符，则截取前200个字符
        if (userAgentHeader.length() > 200) {
            // 在截取的字符串末尾添加省略号，表明被截断了
            return userAgentHeader.substring(0, 200) + "...";
        } else {
            return userAgentHeader;
        }
    }



}
